<?php


namespace app\api\controller;

use app\common\model\Link as L;
use think\exception\ValidateException;
use think\facade\Db;

class Link extends ApiBase
{

    protected $middleware = [
        'app\api\middleware\Auth' => ['except' => ['goodsClass',"list","listAll"] ],
    ];

    public function list()
    {
		$type = input("type");
        $list = L::getList($type);
        return success($list);
    }
	
	public function listAll()
    {
        $list = L::select();
        return success($list);
    }
	
	public function save()
	{
		$data = input("post.");
		$data["uid"] = $this->uid;

		try {
			validate(\app\common\validate\Link::class)->check($data);
		} catch (ValidateException $e) {
			return error($e->getError());
		}

		$id = input("id");
		if (empty($id)) {
			$res = L::create($data);

			if ($res){
				return success(["goods_id" => $res->id],"添加成功");
			}

			return error("添加失败");
		}else{
			$res = L::update($data);

			if ($res){
				return success(["goods_id" => $res->id],"修改成功");
			}

			return error("修改失败");
		}
	}
	
	public function del(){
		$id = input("id");
		$res = L::destroy($id);
		if($res){
			return notice("删除成功");
		}
		
		return error("删除失败");
	}
}